Given two binary strings, return their sum (also a binary string).
The input strings are both non-empty and contains only characters 1
or 0
.
Input: a = "11", b = "1" Output: "100"
Input: a = "1010", b = "1011" Output: "10101"
classSolution: defaddBinary(self, a: str, b: str) ->str: i=-1c='0'ret=""whilei>=-len(a) ori>=-len(b): if (i<-len(a) ora[i] =='0') and (i<-len(b) orb[i] =='0'): ret=c+retc='0'elifi>=-len(a) anda[i] =='1'andi>=-len(b) andb[i] =='1': ret=c+retc='1'elifc=='0': ret='1'+retelse: ret='0'+reti-=1ifc=='1': ret='1'+retreturnret